/*发送消息*/
var params = getParams();
var sendHeadImgUrl = "";
$.get('/users/'+params.id,function (data) {
    $('.header').renderValues(data);
    sendHeadImgUrl = data.headImgUrl;
})
var user = JSON.parse(sessionStorage.getItem('user'));
var getHeadImgUrl = user.headImgUrl;

function send(headSrc,str,time){
	var html="<div class='send'><div class=\"time\">"+time+"</div><div class='msg'><img class='rounded-circle' src="+headSrc+" />"+
	"<p><i class='msg_input'></i>"+str+"</p></div></div>";
	upView(html);
}
/*接受消息*/
function show(headSrc,str,time){
	var html="<div class='show'><div class=\"time\">"+time+"</div><div class='msg'><img class='rounded-circle' src="+headSrc+" />"+
	"<p><i class='msg_input'></i>"+str+"</p></div></div>";
	upView(html);
}
function show1(headSrc,str,time){
	var html="<div class='show'><div class='msg'><img class='rounded-circle' src="+headSrc+" />"+
	"<p><i class='msg_input'></i>"+str+"</p></div></div>";
	upView(html);
}
$.get('/messages/contents',{userId:user.id,sendUserId:params.id},function (data) {
    console.log(data);
    $.each(data,function (index,ele) {
        console.log(ele.sendUser.id);
        if(ele.sendUser.id == params.id){
            send(sendHeadImgUrl,ele.content,ele.createTime);
		}else{
    		show(getHeadImgUrl,ele.content,ele.createTime);
		}
    })
})

/*更新视图*/
function upView(html){
	$('.message').append(html);
	$('body').animate({scrollTop:$('.message').outerHeight()-window.innerHeight},200)
}
function sj(){
	return parseInt(Math.random()*10)
}
$(function(){
	$('.footer').on('keyup','input',function(){
		if($(this).val().length>0){
			$(this).next().css('background','#114F8E').prop('disabled',true);
		
		}else{
			$(this).next().css('background','#ddd').prop('disabled',false);
		}
	})
	$('.footer p').click(function(){
		show1(user.headImgUrl,$(this).prev().val());
        console.log($(this).prev().val());
        $.post("/messages",{'sendUser.id':user.id,user_id:params.id,content:$(this).prev().val()},function (data) {

        })
		//test();
	})
})

/*测试数据*/
var arr=['我是小Q','好久没联系了！','你在想我么','怎么不和我说话','跟我聊会天吧'];
var imgarr=['/icon/touxiang.png','/icon/touxiangm.png']
//test()
function test(){
	$(arr).each(function(i){
		setTimeout(function(){
			send("/icon/touxiang.png",arr[i])
		},sj()*500)
	})
}
